package com.jack.vipmis.vip.api.web.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.jack.vipmis.common.web.controller.BaseController;
import com.jack.vipmis.vip.api.util.MsgUtil;
import com.jack.vipmis.vip.wei.entity.Circle;
import com.jack.vipmis.vip.wei.service.CircleService;

/**
 * 微贴列表Action
 * 
 * @author JackLiang
 * @date 2014-7-7
 * @version 1.0
 */
@Controller
@RequestMapping(value = "api/circle")
public class CircleApiController extends BaseController<Circle, Long> {

	private static final String	DEFAULT_ENCONDING	= "utf-8";

	@Autowired
	private CircleService		circleService;

	/**
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "find")
	@ResponseBody
	public Map<String, Object> getCirCleById(@RequestParam("id") long id) {
		try {
			Circle m = circleService.findOne(id);
			return MsgUtil.doneOk(m);
		} catch (Exception e) {
		}
		return MsgUtil.doneFail();
	}

	/**
	 * 添加
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "add", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> addCircle(HttpServletRequest request) {
		try {
			String postTitle = request.getParameter("postTitle");
			String postContent = request.getParameter("postContent");
			String postPic = request.getParameter("postPic");
			String postUpCount = request.getParameter("postUpCount");
			String userId = request.getParameter("userId");
			String userName = request.getParameter("userName");
			String userAvatar = request.getParameter("userAvatar");
			String isType = request.getParameter("isType");
			String cityName = request.getParameter("cityName");
			// 对象
			Circle circle = new Circle();
			circle.setPostTitle(decode(postTitle));
			circle.setPostContent(decode(postContent));
			circle.setPostPic((postPic == null || postPic.equals("null")) ? "" : postPic);
			circle.setPostUpCount(Long.valueOf(postUpCount == null ? "0" : postUpCount));
			circle.setUserId(Long.valueOf(userId == null ? "" : userId));
			circle.setUserName(decode(userName));
			circle.setUserAvatar(userAvatar == null ? "" : userAvatar);
			circle.setCategory(Long.valueOf(isType == null ? "0" : isType));
			circle.setCityName(decode(cityName));
			Circle m = circleService.saveAndFlush(circle);

			return MsgUtil.doneOk(m);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return MsgUtil.doneFail();
	}

	/**
	 * 分页查询
	 * 
	 * @param page
	 * @param pageSize
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "list")
	@ResponseBody
	public Map<String, Object> getCircleByPage(@RequestParam("page") int page, @RequestParam("pageSize") int pageSize,
			@RequestParam("type") long Category) {
		try {
			Page<Circle> result = circleService.getCircleByPage(page, pageSize, Category);

			return MsgUtil.doneOk(result.getContent());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return MsgUtil.doneFail();

	}

	/**
	 * 
	 * @param value
	 * @return
	 */
	private String decode(String value) {
		try {
			String str = (value == null ? "" : value);
			return URLDecoder.decode(str, DEFAULT_ENCONDING);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return "";
	}
}
